package com.sskj.ffex.v.activity;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.sskj.ffex.R;
import com.sskj.ffex.common.widget.ToolBarLayout;
import com.sskj.ffex.m.bean.ShareInfo;
import com.sskj.ffex.p.activity.SharePresenter;
import com.sskj.ffex.utils.ClickUtil;
import com.sskj.ffex.utils.CopyUtil;
import com.sskj.ffex.utils.ImgUtils;
import com.sskj.ffex.utils.ToastUtil;
import com.sskj.ffex.v.base.BaseActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

import butterknife.BindView;

/**
 * 我的推广
 */
public class ShareActivity extends BaseActivity<SharePresenter> {

    @BindView(R.id.qr_code)
    ImageView qrCode;
    @BindView(R.id.press_save)
    TextView pressSave;

    @BindView(R.id.address)
    TextView address;
    @BindView(R.id.copy_address)
    TextView copyAddress;
    @BindView(R.id.tip)
    TextView tip;
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_share;
    }

    @Override
    public SharePresenter getPresenter() {
        return new SharePresenter();
    }

    @Override
    protected void initView() {
        toolbar.setLeftButtonOnClickLinster(v -> finish());
        //复制二维码
        qrCode.setOnLongClickListener(v -> {
            new RxPermissions(ShareActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(agree -> {
                        if (agree) {
                            if (ImgUtils.saveImageToGallery(ShareActivity.this, qrCode)) {
                                ToastUtil.showShort(getString(R.string.strImgUtils201));
                            } else {
                                ToastUtil.showShort(getString(R.string.strImgUtils202));
                            }
                        } else {
                            ToastUtil.showShort(getString(R.string.strImgUtils202) + getString(R.string.strPermissionTipUtil200));
                        }
                    });

            return true;
        });
        pressSave.setOnLongClickListener(v -> {
            new RxPermissions(ShareActivity.this).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    .subscribe(agree -> {
                        if (agree) {
                            if (ImgUtils.saveImageToGallery(ShareActivity.this, qrCode)) {
                                ToastUtil.showShort(getString(R.string.strImgUtils201));
                            } else {
                                ToastUtil.showShort(getString(R.string.strImgUtils202));
                            }
                        } else {
                            ToastUtil.showShort(getString(R.string.strImgUtils202) + getString(R.string.strPermissionTipUtil200));
                        }
                    });

            return true;
        });
        //复制地址
        ClickUtil.click(copyAddress, () -> CopyUtil.copy(address.getText().toString()));

    }

    @Override
    protected void initData() {
        mPresenter.getShareInfo();
    }

    public void setInfo(ShareInfo data) {
        if (data != null) {
            setText(address, data.getUrl());
            setText(tip, data.getMemo());
            RequestOptions options=new RequestOptions();
            options.skipMemoryCache(true);
            options.placeholder(R.mipmap.ic_launcher);
            Glide.with(this).load(data.getQrc()).apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(qrCode);
        }
    }

}
